package netWork;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 服务器
 */
public class Homework01Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))){
           while (true){
               String request = reader.readLine();
               String response = null;
               if(request.equals("nova")){
                   response = "我喜欢计算机";
               }else if(request.equals("name")){
                   response = "WY";
               }else if(request.equals("0")){
                   response = "0";
               }else {
                   response = "你说啥勒~";
               }
               writer.write(response);
               //证明输入完成
               writer.newLine();
               writer.flush();
               if(response .equals("0")){
                   System.out.println("服务器退出");
                   break;
               }
               System.out.printf("[%s %d],req :%s resp:%s\n",socket.getInetAddress().getHostAddress(),socket.getPort(),request,response);

           }
        }finally {
            socket.close();
            serverSocket.close();
        }

    }

}
